import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

//引入请求数据的模块
import { Http, Jsonp} from '@angular/http';


@IonicPage()
@Component({
  selector: 'page-refresh',
  templateUrl: 'refresh.html',
})
export class RefreshPage {

    public list=[];
    public j=0;

    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                public http: Http,
                public jsonp: Jsonp
    ) {

        for(var i=0;i<5;i++){
            this.list.push(`这是第${i}条数据 `);

        }
    }

    ionViewDidLoad() {
        console.log('ionViewDidLoad RefresherPage');
    }

    doRefresh(refresher){

        //模拟请求数据
        setTimeout(()=>{

            for(var i=0;i<5;i++){
                this.list.unshift(`这是第${this.j}条数据 `);
                this.j++;

            }

            refresher.complete();  /*加载完成以后重新渲染页面*/



        },1000)
    }

    public page=1;

    requestData(infiniteScroll){  /*请求数据*/
        var that=this;
        var url='http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page='+this.page;

        this.http.get(url).subscribe(function(data){
            // console.log(data['_body']);

            var response=JSON.parse(data['_body']).result;

            that.list=that.list.concat(response);   /*拼接每次请求的数据*/

            that.page++;   /*每次请求完成page+1 */

            if(infiniteScroll){
                infiniteScroll.complete();   /*请求完成数据更新页面*/
                if(response<20){   /*没有数据 禁用上拉更新*/
                    infiniteScroll.enable(false);
                }
            }
            console.log('上拉加载成功' + that.page);

        },function(err){

        })

    }

}
